<template>
  <el-dialog title="BOM选择" v-if="showFlag" :visible.sync="showFlag" :modal="false" width="60%" center>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入产品编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品图号" prop="drawNumber">
        <el-input
            v-model="queryParams.drawNumber"
            placeholder="请输入产品图号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table border v-loading="loading" :data="bomList" @current-change="handleCurrent" @row-dblclick="handleRowDbClick">
      <el-table-column width="50" align="center">
        <template v-slot="scope">
          <el-radio v-model="selectedItemId" :label="scope.row.bomId" @change="handleRowChange(scope.row)">
            {{ '' }}
          </el-radio>
        </template>
      </el-table-column>
      <el-table-column label="bom编号" align="center" prop="bomCode"/>
      <el-table-column label="版本号" width="100" align="center" prop="bomName"/>
      <el-table-column
          label="产品图号"
          align="center"
          key="drawNumber"
          prop="drawNumber"
          :show-overflow-tooltip="true"
      />
      <el-table-column label="产品编码" width="140" align="center" prop="itemCode"/>
      <el-table-column label="产品名称" align="center" prop="itemName" :show-overflow-tooltip="true"/>
      <el-table-column label="产品类别" align="center" prop="bomDefault" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.bomDefault === 0" type="success">成品</el-tag>
          <el-tag v-else>半成品</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="规格型号" align="center" prop="specification" :show-overflow-tooltip="true"/>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listBom } from '@/api/mes/md/bomgl';

export default {
  name: 'BomSelectSinglegh',
  dicts: ['bom_default'],
  data() {
    return {
      showFlag: false,
      // 遮罩层
      loading: true,
      selectedItemId: undefined,
      selectedRows: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品BOM关系表格数据
      bomList: [],
      // 弹出层标题
      title: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: '发布',
        itemCode: '',
        itemName: '',
        ancestors: 1,
        drawNumber: '',
      }
    };
  },

  created() {
    this.getList();
  },
  methods: {
    /** 查询产品BOM关系列表 */
    getList() {
      this.loading = true;
      listBom(this.queryParams).then(response => {
        this.bomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleCurrent(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    handleRowDbClick(row) {
      if (row) {
        this.selectedRows = row;
        this.$emit('onSelected', this.selectedRows);
        this.showFlag = false;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if (row) {
        this.selectedRows = row;
      }
    },

    //确定选中
    confirmSelect() {
      if (this.selectedItemId == null || this.selectedItemId == 0) {
        this.$notify({
          title: '提示',
          type: 'warning',
          message: '请至少选择一条数据!'
        });
        return;
      }
      this.$emit('onSelected', this.selectedRows);
      this.showFlag = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
  }
};
</script>
